//
//  DetailCheckInViewController.m
//  Spa
//
//  Created by PangPond on 14/1/2556.
//  Copyright (c) พ.ศ. 2556 innova. All rights reserved.
//

#import "DetailCheckInViewController.h"
#import "SelectMenuViewController.h"
#import "SelectStaffViewController.h"
#import "SelectBedViewController.h"
@interface DetailCheckInViewController ()

@end

@implementation DetailCheckInViewController

@synthesize keyCustomer;

//Time
@synthesize datePicker,labelText,selected;
@synthesize detailTableView = _detailTableView;
@synthesize btnTime;
@synthesize datelabel;
//Cell
@synthesize checkInFieldCustomerCell = _checkInFieldCustomerCell;

@synthesize checkInFieldStaffCell = _checkInFieldStaffCell;
@synthesize btnStaff;
@synthesize selectStaff;
@synthesize keySelectRowStaff;

@synthesize checkInFieldBedCell = _checkInFieldBedCell;
@synthesize btnBed;
@synthesize selectBed;
@synthesize keySelectRowBed;


@synthesize checkInFieldMenuCell = _checkInFieldMenuCell;
@synthesize btnMenu;
@synthesize pickedTags;


@synthesize checkInFieldTimeCell = _checkInFieldTimeCell;

@synthesize checkInFieldPriceCell = _checkInFieldPriceCell;
@synthesize priceLabel;
@synthesize selectItem;
@synthesize selectMenu;
@synthesize keySelectMenu;
@synthesize price;
- (IBAction)dismissKeyboard:(id)sender
{
	[sender resignFirstResponder];
    
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.databaseUtility = [[DatabaseUtility alloc]initWithDatabaseName:@"Database"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - UITableViewDataSource

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    //[self.tableView reloadData];
    [self loadView];
    [self viewDidLoad];
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 6;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    //NSArray *customer = [self.databaseUtility fetchedResultsWithClass:@"Customer" withSortDescriptorString:@"firstname" withPredicateString:[NSString stringWithFormat:@"firstname == '%@'",self.keyCustomerFirstname]];
    
   // Customer *customers = [customer lastObject];
    
    if (indexPath.row == 0) {
        static NSString *CellIdentifier = @"CheckInFieldCustomerCell";
        
        CheckInFieldCustomerCell *cell = (CheckInFieldCustomerCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            
            [[NSBundle mainBundle] loadNibNamed:@"CheckInFieldCustomerCell" owner:self options:nil];
            
            cell = self.checkInFieldCustomerCell;
            self.checkInFieldCustomerCell = nil;
        }
        
        //config Cell
        cell.textFieldCell.text = keyCustomer;
        cell.textFieldCell.enabled = NO;
        return cell;
        
    }else if (indexPath.row == 1){
        static NSString *CellIdentifier = @"CheckInFieldMenuCell";
        
        CheckInFieldMenuCell *cell = (CheckInFieldMenuCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            
            [[NSBundle mainBundle] loadNibNamed:@"CheckInFieldMenuCell" owner:self options:nil];
            
            cell = self.checkInFieldMenuCell;
            self.checkInFieldMenuCell = nil;
        }
        
        //config Cell
        if (selectItem == NULL) {
            selectItem = 0;
        }else{
            cell.textFieldCell.text = [NSString stringWithFormat:@"%d Select Item",(int *)selectItem];
        }
        
        cell.textFieldCell.enabled = NO;
        [btnMenu addTarget:self action:@selector(SelectMenu:) forControlEvents:UIControlEventTouchUpInside];
        
        return cell;
        
    }else if (indexPath.row == 2){
        static NSString *CellIdentifier = @"CheckInFieldStaffCell";
        
        CheckInFieldStaffCell *cell = (CheckInFieldStaffCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            
            [[NSBundle mainBundle] loadNibNamed:@"CheckInFieldStaffCell" owner:self options:nil];
            
            cell = self.checkInFieldStaffCell;
            self.checkInFieldStaffCell = nil;
        }
        
        //config Cell
        cell.textFieldCell.text = selectStaff;
        cell.textFieldCell.enabled = NO;
        [btnStaff addTarget:self action:@selector(SelectStaff:) forControlEvents:UIControlEventTouchUpInside];
        
        return cell;
    }else if (indexPath.row == 3){
        static NSString *CellIdentifier = @"CheckInFieldBedCell";
        
        CheckInFieldBedCell *cell = (CheckInFieldBedCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            
            [[NSBundle mainBundle] loadNibNamed:@"CheckInFieldBedCell" owner:self options:nil];
            
            cell = self.checkInFieldBedCell;
            self.checkInFieldBedCell = nil;
        }
        
        //config Cell
        cell.textFieldCell.text = selectBed;
        cell.textFieldCell.enabled = NO;
        [btnBed addTarget:self action:@selector(SelectBed:) forControlEvents:UIControlEventTouchUpInside];
        
        return cell;
    }else if (indexPath.row == 4){
        static NSString *CellIdentifier = @"CheckInFieldTimeCell";
        
        CheckInFieldTimeCell *cell = (CheckInFieldTimeCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            
            [[NSBundle mainBundle] loadNibNamed:@"CheckInFieldTimeCell" owner:self options:nil];
            
            cell = self.checkInFieldTimeCell;
            self.checkInFieldTimeCell = nil;
        }
        
        //config Cell
        
        NSDateFormatter *currentDate = [[NSDateFormatter alloc] init];
        [currentDate setDateFormat:@"dd-MM-yyyy h:mm a"];
        
        NSLog(@"Time1 %@",[NSString stringWithFormat:@"%@",[currentDate stringFromDate:[NSDate date]]]);
        
        cell.textFieldCell.text = [NSString stringWithFormat:@"%@",[currentDate stringFromDate:[NSDate date]]];
        cell.textFieldCell.enabled = NO;
        [btnTime addTarget:self action:@selector(Date) forControlEvents:UIControlEventTouchUpInside];
        
        return cell;
    }else if (indexPath.row == 5){
        static NSString *CellIdentifier = @"CheckInFieldPriceCell";
        
        CheckInFieldPriceCell *cell = (CheckInFieldPriceCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            
            [[NSBundle mainBundle] loadNibNamed:@"CheckInFieldPriceCell" owner:self options:nil];
            
            cell = self.checkInFieldPriceCell;
            self.checkInFieldPriceCell = nil;
        }
        
        //config Cell
        
        //NSLog(@"Test %d",price);
        cell.textFieldCell.text = [NSString stringWithFormat:@"%d บาท",(int *)price];
        cell.textFieldCell.enabled = NO;
              
        return cell;
    }
    
}

//Delegate
-(void)changeTextsMenu:(int *)textString{
    selectItem = (int *)textString;
    
}
-(void)priceMenu:(int *)textString{
    price =textString;
}

-(void)changeTextStaff:(NSString *)textString{
    selectStaff = textString;
}

-(void)changeTextBed:(NSString *)textString{
    selectBed = textString;
}
-(void)selectMenu:(int *)textString{
    selectMenu = (int *)textString;
    
}
-(void)keySelectMenu:(NSString *)textString{
    //keySelectMenu = textString;
         NSLog(@"%@ ",textString);
}
-(void)keySelectRowStaff:(int *)keyint{
    keySelectRowStaff = keyint;
}
-(void)keySelectRowBed:(int *)keyint{
    keySelectRowBed = keyint;
}
-(void)pickedTags:(NSMutableSet *)setString{
    pickedTags = setString;
}
//validation




//Animate


//Function
-(void)SelectBed:(id)sender{
    SelectBedViewController *selectBedViewController = [[SelectBedViewController alloc] initWithNibName:@"SelectBedViewController" bundle:nil];
    selectBedViewController.keySelectRowBed = keySelectRowBed;
    selectBedViewController.delegate = self;
    [self presentViewController:selectBedViewController animated:YES completion:nil];
}

-(void)SelectStaff:(id)sender{
    SelectStaffViewController *selectStaffViewController = [[SelectStaffViewController alloc] initWithNibName:@"SelectStaffViewController" bundle:nil];
    selectStaffViewController.keySelectRowStaff =keySelectRowStaff;
    selectStaffViewController.delegate = self;
    [self presentViewController:selectStaffViewController animated:YES completion:nil];
}

-(void)SelectMenu:(id)sender{
    SelectMenuViewController *selectMenuViewController = [[SelectMenuViewController alloc] initWithNibName:@"SelectMenuViewController" bundle:nil];
    
   // if (keySelectMenu = NULL) {
        //selectMenu = 1;
    //}else{
        
   // }
    selectMenuViewController.pickedTags =pickedTags;
    selectMenuViewController.keySelectMenu =keySelectMenu;
    selectMenuViewController.selectMenu =selectMenu;
    selectMenuViewController.delegate = self;
    [self presentViewController:selectMenuViewController animated:YES completion:nil];
}
-(IBAction)SaveButton:(id)sender{
    
}
-(IBAction)BackButton:(id)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}

////////////////////date



- (void)Date{

	//NSDateFormatter *df = [[NSDateFormatter alloc] init];
	//df.dateStyle = NSDateFormatterMediumStyle;

  
    NSDateFormatter *currentDate = [[NSDateFormatter alloc] init];
    [currentDate setDateFormat:@"dd-MM-yyyy h:mm:ss a"];
    
      NSLog(@"Time1 %@",[NSString stringWithFormat:@"%@",[currentDate stringFromDate:[NSDate date]]]);
    
	//[self.view addSubview:datelabel];
	
	
	datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 325, 300)];
	datePicker.datePickerMode = UIDatePickerModeDateAndTime;
	datePicker.hidden = NO;
	datePicker.date = [NSDate date];
    
	[datePicker addTarget:self
	               action:@selector(LabelChange:)
	     forControlEvents:UIControlEventValueChanged];
    
    //self.detailTableView.userInteractionEnabled = YES;
   //UITapGestureRecognizer *dateTapRecognizer1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(Hide:)];
    //[self.detailTableView addGestureRecognizer:dateTapRecognizer1];
    
	[self.view addSubview:datePicker];

}
- (void)LabelChange:(id)sender{
    NSDateFormatter *currentDate = [[NSDateFormatter alloc] init];
    [currentDate setDateFormat:@"dd-MM-yyyy h:mm:ss a"];
    

	datelabel = [NSString stringWithFormat:@"%@",[currentDate stringFromDate:datePicker.date]];
    NSLog(@"Time2 %@",datelabel);
}

-(IBAction)Hide:(id)sender{
    datePicker.hidden = YES;
    
    
}

@end
